<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    排序方式 - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						排序方式
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/01/12 00:00 上午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#C语言</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<h2><a id="%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F%EF%BC%9A" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>冒泡排序：</h2>
<ul>
<li>从前往后比较相邻元素，如果后者比前者大，交换二者位置</li>
<li>对第一组开始到最后一组的每一组相邻元素执行相同操作</li>
<li>共进行N-1轮排序</li>
</ul>
<pre class="line-numbers"><code class="language-c">int main()
{
    int a[6] = {15,20,7,12,9,1};
    int i,j=0;

    for(i=0;i&lt;=4;i++){
        for(j=0;j&lt;6-i-1;j++){
            if (a[j] &gt; a[j + 1])
            {
                int b = a[j];
                a[j] = a[j+1];
                a[j+1] = b;
            };
        }
    }

    for(i=0;i&lt;6;i++){
        printf(&quot;%d &quot;,a[i]);
    }
    return 0;
}
</code></pre>
<h2><a id="%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F%EF%BC%9A" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>选择排序：</h2>
<ul>
<li>每一轮都从前往后寻找最小元素，并与相应位置的元素进行对调</li>
<li>从第一个元素开始，对之后每一个元素执行以上操作</li>
<li>共进行N-1轮排序，每轮排序中将最小值与a[N]进行交换</li>
</ul>
<pre class="line-numbers"><code class="language-c">int main()
{
    int a[6] = {15,20,7,12,9,1};
    int i,j=0;
    int min,pos;

    for(i=0;i&lt;6-1;i++){
        min=a[i];
        pos=i;
        for(j=i+1;j&lt;6;j++){
            if(a[j]&lt;min){
                min=a[j];
                pos=j;
            }
        }
        if(pos!=i){
            int t=a[i];
            a[i]=a[pos];
            a[pos]=t;
        }
    }

    for(i=0;i&lt;6;i++){``
        printf(&quot;%d &quot;,a[i]);
    }
    return 0;
}
</code></pre>
<h1><a id="%E6%8A%98%E5%8D%8A%E6%9F%A5%E6%89%BE%EF%BC%88%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE%EF%BC%89" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>折半查找（二分查找）</h1>
<p>每次将数组内的数据对半查找，直至得到最终结果</p>
<pre class="line-numbers"><code class="language-c">int main()
{
   int a[6] = {1,3,11,20,21,90};
   int l,h,m;
   int val,pos=-1;

   printf(&quot;val = &quot;);
   scanf(&quot;%d&quot;,&amp;val);

   l=0;
   h=6-1;
   while(l&lt;=h){
       m=(l+h)/2;
       if(val==a[m]){
           pos=m;
           break;
       }else if(val&lt;a[m]){
           h=m-1;
       }else{
           l=m+1;
       }
   }
   if(pos==-1){
       printf(&quot;no find&quot;);
   }else{
       printf(&quot;a[%d]=%d&quot;,pos,val);
   }
   return 0;
}
</code></pre>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>